package com.gf.common;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class EncodingUtil {
	
	private static final Logger LOGGER = LoggerFactory.getLogger(EncodingUtil.class);
	
	private static EncodingUtil instance = null;
	
	private EncodingUtil() {
		
	}
	
	public static EncodingUtil getInstance() {
		if(null == instance) {
			instance = new EncodingUtil();
		}
		return instance;
	}
	
	/**
	 * 
	 * @description UTF-8解码
	 * @return String
	 */
	public String decode(String str) {
		try {
			if(str==null) str="";
			str = URLDecoder.decode(str, "UTF-8");
		} catch (UnsupportedEncodingException e) {
			LOGGER.error("字符解码异常：", e);
		}
		
		return str;
	}
	
	/**
	 * 
	 * @description ISO-8859-1编码的字符转换成GBK
	 * @return String
	 */
	public String encodeISOToGBK(String str) {
		try {
			str = new String(str.getBytes("ISO-8859-1"), "GBK");
		} catch (UnsupportedEncodingException e) {
			LOGGER.error("ISO-8859-1转换成GBK异常：", e);
		}
		return str;
	}
}
